<?php
namespace Admin\Model;
use Think\Model;

class UserModel extends Model{
    protected $connection = 'DB_CONFIG2';

	/**
	 * 根据条件，返回订单总数
	 * @param string $where
	 * @return number|NULL
	 */
	public function getUserCount($where=null){
		$orderNum = $this->alias('u')->where($where)->select();
		if ($orderNum) {
			$orderNum = count($orderNum);
			return $orderNum;
		}else{
			return null;
		}
	}

	/**
	 * 获取订单列表，并分页，返回订单列表
	 * @param unknown $page
	 * @param string $where
	 * @return unknown|NULL
	 */
	public function getUserList($page,$where=null,$p=null,$type="select"){
	    if($type=="select"){
	        $orderList = $this->field('u.*')->alias('as u')->
	        where($where)->order('u.create_time desc')->page($p)->select();
	    }
	    else if($type=="find"){
	        $orderList = $this->field('u.*')->alias('as u')->
	        where($where)->order('u.create_time desc')->find();
	    }

		if ($orderList) {
			return $orderList;
		}else{
			return null;
		}
	}

	/**
	 * 根据条件，返回订单总数
	 * @param string $where
	 * @return number|NULL
	 */
	public function getUserServerCount($where=null){
		$orderNum = $this->alias('u')->
		join(C('DB_PREFIX').'order_info as oi ON oi.openid = u.openid')->
		join(C('DB_PREFIX').'order as o ON o.ordersn = oi.ordersn')->
		where($where)->count('distinct u.id');
		if ($orderNum) {
			// $orderNum = count($orderNum);
			return $orderNum;
		}else{
			return null;
		}
	}

	/**
	 * 获取订单列表，并分页，返回订单列表
	 * @param unknown $page
	 * @param string $where
	 * @return unknown|NULL
	 */
	public function getUserServerList($page,$where=null,$p=null,$type="select"){
	    if($type=="select"){
	        $orderList = $this->distinct(true)->field('u.*,o.card_type')->alias('as u')->
			join(C('DB_PREFIX').'order_info as oi ON oi.openid = u.openid')->
			join(C('DB_PREFIX').'order as o ON o.ordersn = oi.ordersn')->
	        where($where)->order('u.create_time desc')->page($p)->select();
	    }
	    else if($type=="find"){
	        $orderList = $this->distinct(true)->field('u.*,o.card_type')->alias('as u')->
			join(C('DB_PREFIX').'order_info as oi ON oi.openid = u.openid')->
			join(C('DB_PREFIX').'order as o ON o.ordersn = oi.ordersn')->
	        where($where)->order('u.create_time desc')->find();
	    }

		if ($orderList) {
			return $orderList;
		}else{
			return null;
		}
	}

	/**
	 * 根据条件，返回订单总数
	 * @param string $where
	 * @return number|NULL
	 */
	public function getUserMonthCount($where=null){
		$orderNum = $this->alias('u')->
		join(C('DB_PREFIX').'month_order as mo ON mo.openid = u.openid')->
		where($where)->count('distinct u.id');
		if ($orderNum) {
			return $orderNum;
		}else{
			return null;
		}
	}

	/**
	 * 获取月卡订单列表，并分页，返回订单列表
	 * @param unknown $page
	 * @param string $where
	 * @return unknown|NULL
	 */
	public function getUserMonthList($page,$where=null,$p=null,$type="select"){
		if($type=="select"){
	        $orderList = $this->distinct(true)->field('u.*')->alias('as u')->
			join(C('DB_PREFIX').'month_order as mo ON mo.openid = u.openid')->
	        where($where)->order('u.create_time desc')->page($p)->select();
	    }
	    else if($type=="find"){
	        $orderList = $this->distinct(true)->field('u.*')->alias('as u')->
			join(C('DB_PREFIX').'month_order as mo ON mo.openid = u.openid')->
	        where($where)->order('u.create_time desc')->find();
	    }

		if ($orderList) {
			return $orderList;
		}else{
			return null;
		}
	}

}

?>
